package com.k8s.controller;

import com.k8s.config.AutoRefreshConfig;
import com.k8s.domain.K8sProperties;
import com.k8s.service.MonitorK8sService;
import com.k8s.utils.IpUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = {"/monitor"})
public class MonitorK8sController {
    private final static Logger LOGGER = LoggerFactory.getLogger(MonitorK8sController.class);
    @Resource
    private MonitorK8sService monitorK8sService;

    @Resource
    private AutoRefreshConfig config;
    
    /**
     * 这是一个k8s监控工具，当pod出现重启的时候进行短信预警通知。
     * 一分钟扫描一次。
     * @param monitorK8s
     * @param stay
     * @param request
     * @return
     */
    @GetMapping(value = "/monitorK8s")
    @ResponseBody
    public Map analysis(@RequestParam(value = "monitorK8s", required = false, defaultValue = "manual") String monitorK8s,
                        @RequestParam(value = "stay", required = false) String stay,
                        HttpServletRequest request) {
        LOGGER.info("进入analysis......");
        List<String> whiteIps = config.getWhiteIps();
        String ipAddress = IpUtil.getIpAddress(request);
        LOGGER.info("当前调用接口的IP地址: {}", ipAddress);
//        if (CollectionUtils.isEmpty(whiteIps) || !whiteIps.contains(ipAddress)) {
//            resultMap.put("msg", "没有权限访问");
//            return resultMap;
//        }

        return monitorK8sService.analysis(monitorK8s, request, stay);
    }

    /**
     * 查询数据
     *
     * @return
     */
    @GetMapping(value = "/selectPodList")
    @ResponseBody
    public List<K8sProperties> selectPodList() {
        return monitorK8sService.selectPodList();
    }

    /**
     * 查询数据
     *
     * @return
     */
    @GetMapping(value = "/selectPodPageList")
    public String selectPodPageList() {
        return "index";
    }

}
